﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataEntity;
using JScripter;
using BusinessLogic.Controller;
using BusinessLogic.Helper;

public partial class AjaxPages_User_Profile : AjaxPage
{
    private Guid ID
    {
        get
        {
            if (Request.Params["cid"] != null || AjaxState.ContainsKey("cid"))
            {
                if (!AjaxState.ContainsKey("cid"))
                    AjaxState["cid"] = Request.Params["cid"];
                return new Guid(AjaxState["cid"]);
            }
            else if (new LoginHelper().LoginID != Guid.Empty)
            {
                return new LoginHelper().LoginID;
            }
            else
            {
                return Guid.Empty;
            }
        }
    }


    protected override void OnLoad(EventArgs e)
    {
        if (ID != Guid.Empty)
        {
            BindData();
            objLoader.DropDownChangeEventLoadAnotherDropDown(ddState, "#divCity", ResolveUrl("~/AjaxPages/ControlLoader/DControl.aspx") + "?sid=");
        }
        base.OnLoad(e);
    }
    private void EditData()
    {

        LoginUser objLoginUser = new LoginUser();

        objLoginUser.LoginID = ID;

        //objLoginUser.Username = HtmlHelper.ControlValue(txtUsername.ClientID);


        objLoginUser.FirstName = HtmlHelper.ControlValue(txtFirstName.ClientID);

        objLoginUser.LastName = HtmlHelper.ControlValue(txtLastName.ClientID);

        objLoginUser.MiddleName = HtmlHelper.ControlValue(txtMiddleName.ClientID);

        objLoginUser.Address1 = HtmlHelper.ControlValue(txtAddress1.ClientID);

        objLoginUser.Address2 = HtmlHelper.ControlValue(txtAddress2.ClientID);

        objLoginUser.City = new CityController().GetbyCityID(new Guid(HtmlHelper.ControlValue(ddCity.ClientID)))[0].CityName;

        objLoginUser.State = new StateController().GetbyStateID(new Guid(HtmlHelper.ControlValue(ddState.ClientID)))[0].Name;

        objLoginUser.Pincode = HtmlHelper.ControlValue(txtPincode.ClientID);

        objLoginUser.Occupation = HtmlHelper.ControlValue(txtOccupation.ClientID);

        objLoginUser.HouseholdIncome = HtmlHelper.ControlValue(txtHouseholdIncome.ClientID);

        objLoginUser.ModifiedDate = DateTime.Now;



        new LoginUserController().UpdateByLoginID(objLoginUser);
        Response.Redirect("~/College/Ajaxer/CourceCatagoryInfoView.aspx");
    }

    private void BindData()
    {
        var dataBunch = new LoginUserController().GetbyLoginID(ID);
        if (dataBunch.Count > 0)
        {
            var data = dataBunch[0];

            txtUsername.Text = data.Username;


            txtFirstName.Text = data.FirstName;

            txtLastName.Text = data.LastName;

            txtMiddleName.Text = data.MiddleName;

            txtAddress1.Text = data.Address1;

            txtAddress2.Text = data.Address2;
            if (data.State != null)
            {
                new StateController().BindStateSelectByText(ddState, data.State);
                new CityController().BindCitySelectByText(ddCity, data.State, data.City);
            }
            else
            {
                new StateController().BindState(ddState);
                ddCity.Enabled = false;
            }


            txtPincode.Text = data.Pincode;

            txtOccupation.Text = data.Occupation;

            txtHouseholdIncome.Text = data.HouseholdIncome;

        }
    }

    protected void AddAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        // AddData();

    }
    protected void UpdateAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        EditData();
    }
    public void PreventData()
    {
        txtUsername.Text = HtmlHelper.ControlValue(txtUsername.ClientID);
        txtFirstName.Text = HtmlHelper.ControlValue(txtFirstName.ClientID);
        txtLastName.Text = HtmlHelper.ControlValue(txtLastName.ClientID);
        txtMiddleName.Text = HtmlHelper.ControlValue(txtMiddleName.ClientID);
        txtAddress1.Text = HtmlHelper.ControlValue(txtAddress1.ClientID);
        txtAddress2.Text = HtmlHelper.ControlValue(txtAddress2.ClientID);

        new StateController().BindState(ddState, HtmlHelper.ControlValue(ddState.ClientID));
        new CityController().BindCity(ddCity, new Guid(HtmlHelper.ControlValue(ddState.ClientID)), HtmlHelper.ControlValue(ddCity.ClientID));
        txtPincode.Text = HtmlHelper.ControlValue(txtPincode.ClientID);
        txtOccupation.Text = HtmlHelper.ControlValue(txtOccupation.ClientID);
        txtHouseholdIncome.Text = HtmlHelper.ControlValue(txtHouseholdIncome.ClientID);


    }

}